package com.proton.carepatchtemp.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.MainActivity;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.login.fragment.Guide1Fragment;
import com.proton.carepatchtemp.login.fragment.Guide2Fragment;
import com.proton.carepatchtemp.login.fragment.Guide3Fragment;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.PrfCenter;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.ArrayList;


public class GuideActivity extends BaseActivity implements View.OnClickListener {
    Profile mProfile;
    private ImageButton mBtn_back;
    private TextView mTv_skip_guide;
    private android.support.v4.view.ViewPager mGuide_container;
    private TextView mTv_next_guide;
    private ImageView mGuide_point1;
    private ImageView mGuide_point2;
    private ImageView mGuide_point3;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter myPagerAdapter;
    private ViewPagerChangeListener mViewPagerChangeListener;


    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.NET_PRF_GETLIST:
                finish();
                break;
            case Codes.POST_BIRTH:
                String str = (String) event.getObj();
                mProfile.setBirthday(str);
                break;
            case Codes.POST_MAN:
                mProfile.setGender(1);
                break;
            case Codes.POST_WOMAN:
                mProfile.setGender(2);
                break;
            case Codes.POST_NAME:
                String strName = (String) event.getObj();
                mProfile.setRealname(strName);
                mProfile.setTitle(strName);
                break;
            case Codes.POST_AVATOR:
                String strAvator = (String) event.getObj();
                mProfile.setAvatar(strAvator);
                break;
            case Codes.NET_PRF_ADD:
                if (event.isSuccess()) {
                    //LogUtils.e(event.getMsg());
                    Intent goMain = new Intent(GuideActivity.this, MainActivity.class);
                    startActivity(goMain);
                } else {
                    Toast.makeText(this, event.getMsg(), Toast.LENGTH_SHORT).show();
                    mGuide_container.setCurrentItem(0);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void setListener() {
        mTv_skip_guide.setOnClickListener(v -> {
            Intent goMain = new Intent(GuideActivity.this, MainActivity.class);
            startActivity(goMain);
        });
        mBtn_back.setOnClickListener(v -> {
            if (mGuide_container.getCurrentItem() == 0) {
                finish();
            } else if (mGuide_container.getCurrentItem() == 1) {
                mGuide_container.setCurrentItem(0);
            } else if (mGuide_container.getCurrentItem() == 2) {
                mGuide_container.setCurrentItem(1);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_pager_container);
        bindViews();
        initViewPagerPoint();
        initViewPager();
        setListener();
        mProfile = new Profile();
    }

    // End Of Content View Elements
    private void bindViews() {
        mBtn_back = findViewById(R.id.btn_back);
        mTv_skip_guide = findViewById(R.id.tv_skip_guide);
        mGuide_container = findViewById(R.id.guide_container);
        mTv_next_guide = findViewById(R.id.tv_next_guide);
        mGuide_point1 = findViewById(R.id.guide_point1);
        mGuide_point2 = findViewById(R.id.guide_point2);
        mGuide_point3 = findViewById(R.id.guide_point3);
    }

    @Override
    public void onBackPressed() {
        if (mGuide_container.getCurrentItem() == 0) {
            finish();
        } else if (mGuide_container.getCurrentItem() == 1) {
            mGuide_container.setCurrentItem(0);
        } else if (mGuide_container.getCurrentItem() == 2) {
            mGuide_container.setCurrentItem(1);
        }
    }

    /**
     * 初始化导航圆点
     */
    private void initViewPagerPoint() {
        mGuide_point1.setImageResource(R.mipmap.pointoff);
        mGuide_point2.setImageResource(R.mipmap.pointoff);
        mGuide_point3.setImageResource(R.mipmap.pointoff);
    }

    private void initViewPager() {
        initViewPagerPoint();
        mGuide_point1.setImageResource(R.mipmap.pointon);

        //  Fragment数组
        Fragment guide1Fragment = Guide1Fragment.newInstance();
        Fragment guide2Fragment = Guide2Fragment.newInstance();
        Fragment guide3Fragment = Guide3Fragment.newInstance();
        mFragments.add(guide1Fragment);
        mFragments.add(guide2Fragment);
        mFragments.add(guide3Fragment);

        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewPagerChangeListener = new ViewPagerChangeListener();
        mGuide_container.setAdapter(myPagerAdapter);
        mGuide_container.addOnPageChangeListener(mViewPagerChangeListener);
        mGuide_container.setOffscreenPageLimit(2);
        mTv_next_guide.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mProfile == null) return;
        switch (v.getId()) {
            case R.id.tv_next_guide:
                int nowTtem = mGuide_container.getCurrentItem();
                switch (nowTtem) {
                    case 0:
                        if (TextUtils.isEmpty(mProfile.getRealname())) {
                            TastyUtils.makeTextShort("请输入姓名！", TastyToast.CONFUSING);
                            return;
                        }
                        if (TextUtils.isEmpty(mProfile.getBirthday())) {
                            TastyUtils.makeTextShort("请选择出生日期！", TastyToast.CONFUSING);
                            return;
                        }
                        mGuide_container.setCurrentItem(1);
                        break;
                    case 1:
                        if (TextUtils.isEmpty(mProfile.getAvatar())) {
                            TastyUtils.makeTextShort(R.string.complete_prf_pic, TastyToast.CONFUSING);
                            return;
                        }
                        mGuide_container.setCurrentItem(2);
                        break;
                    case 2:
                        //判断数据
                        if (TextUtils.isEmpty(mProfile.getRealname()) || TextUtils.isEmpty(mProfile.getBirthday())) {
                            Toast.makeText(this, R.string.complete_prf_info, Toast.LENGTH_LONG).show();
                            mGuide_container.setCurrentItem(0);
                            mTv_next_guide.setText(R.string.next_step);
                            return;
                        }
//                        if(TextUtils.isEmpty(mProfile.getAvatar())){
//                            Toast.makeText(this, R.string.complete_prf_pic, Toast.LENGTH_LONG).show();
//                            mGuide_container.setCurrentItem(1);
//                            mTv_next_guide.setText(R.string.next_step);
//                            return;
//                        }
                        PrfCenter.add(mProfile);
                        break;
                    default:
                        break;
                }
        }
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private boolean isGetFullDatasFromGuide1() {
        return false;
    }

    /**
     * ViewPager适配器
     */
    private static class MyPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> mFragments;

        public MyPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
            super(fm);
            this.mFragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

    }

    /**
     * ViewPager监听器
     */
    private class ViewPagerChangeListener implements ViewPager.OnPageChangeListener {

        int pagerPosition = 0;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            pagerPosition = position;

            switch (position) {
                case 0:
                    initViewPagerPoint();
                    mGuide_point1.setImageResource(R.mipmap.pointon);
                    mTv_skip_guide.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    initViewPagerPoint();
                    mGuide_point2.setImageResource(R.mipmap.pointon);
                    mTv_skip_guide.setVisibility(View.VISIBLE);
                    mTv_next_guide.setText(R.string.next_step);
                    break;
                case 2:
                    initViewPagerPoint();
                    mGuide_point3.setImageResource(R.mipmap.pointon);
                    mTv_skip_guide.setVisibility(View.INVISIBLE);
                    mTv_next_guide.setText(R.string.finished);
                    new MainEvent(Codes.NET_SIGN_GUIDE, mProfile).post();
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


}
